package com.wanggsx.library.base.mvp;

import android.app.Activity;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;

import java.lang.ref.WeakReference;

public abstract class BasePresenter<AF,V extends IView> {
    //json解析
    protected Gson mGson;

    private WeakReference<AF> mRefFragmentOrActivity;

    public BasePresenter(AF fragmentOrActivity) {
        this.mGson = new Gson();
        if (fragmentOrActivity!=null && (fragmentOrActivity instanceof Activity || fragmentOrActivity instanceof Fragment) ) {
            this.mRefFragmentOrActivity = new WeakReference<>(fragmentOrActivity);
        }
    }

    public V getIView() {
        AF fragmentOrActivity = getFragmentOrActivity();
        return (V)(fragmentOrActivity instanceof IView?fragmentOrActivity:null);
    }

    public AF getFragmentOrActivity(){
        if (mRefFragmentOrActivity!=null){
            return mRefFragmentOrActivity.get();
        }
        return null;
    }

    void detachFragmentOrActivity(){
        if (mRefFragmentOrActivity!=null){
            mRefFragmentOrActivity.clear();
            mRefFragmentOrActivity = null;
        }
    }
    
}
